package com.shizhanzhe.zxbapp.vip;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.Notice;
import com.shizhanzhe.zxbapp.ui.RegisterActivity;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;

import java.util.ArrayList;
import java.util.List;

import xcs.ert.jlj.os.df.DiyOfferWallManager;

/**
 * Created by zz9527 on 2018/11/12.
 */
public class NoticeActivity extends Activity {
    private int page = 1;
    private ListView lv;
    private View nodata;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);
        ImmersionBar.with(this).statusBarColor(R.color.them)
                .fitsSystemWindows(true).init();
        final int type = getIntent().getIntExtra("type", 0);
        findViewById(R.id.backarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        nodata = findViewById(R.id.nodata);
        TextView title = findViewById(R.id.top_title);
        lv = findViewById(R.id.lv);
        SmartRefreshLayout refresh = findViewById(R.id.refresh);
        //设置 Header 样式
        refresh.setRefreshHeader(new MaterialHeader(this));
//        //设置 Footer 样式
//        refresh.setRefreshFooter(new ClassicsFooter(getContext()));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                list.clear();
                getData(type);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                refreshLayout.finishLoadMore(2000/*,false*/);//传入false表示刷新失败
                getData(type);
            }
        });
        if (type == 1) {
            title.setText("新手学堂");
        } else if (type == 2) {
            title.setText("规则中心");
        }
        getData(type);
    }
    private List<Notice.DataBean> list=new ArrayList<>();
    private void getData(final int type) {
        String path = "";
        if (type == 1) {
            path = Path.XS(page);
        } else if (type == 2) {
            path = Path.GZ(page);
        }
        OkHttpDownloadJsonUtil.downloadJson(this, path, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        Gson gson = new Gson();
                        List<Notice.DataBean> data = gson.fromJson(json, Notice.class).getData();
                        list.addAll(data);
                        if (list.size() == 0) {
                            nodata.setVisibility(View.VISIBLE);
                            lv.setVisibility(View.GONE);
                        }else if (data.size()==0){
                            Toast.makeText(NoticeActivity.this, "没有更多了", Toast.LENGTH_SHORT).show();
                        }
                        lv.setAdapter(new NoticeAdapter(NoticeActivity.this, list));
                        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                Intent intent = new Intent(NoticeActivity.this, NoticeDetailActivity.class);
                                intent.putExtra("title", list.get(i).getTitle());
                                intent.putExtra("id", list.get(i).getId());
                                intent.putExtra("type", type);
                                startActivity(intent);
                            }
                        });
                    } catch (Exception e) {

                    }
                } else {
                    Toast.makeText(NoticeActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy(); //必须调用该方法，防止内存泄漏
    }
}
